package com.ouheng.shop.controller;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.common.annotation.Log;
import com.ouheng.common.utils.PageUtils;
import com.ouheng.common.utils.Query;
import com.ouheng.common.utils.R;
import com.ouheng.common.utils.ServletsUtils;
import com.ouheng.shop.domain.ArticleImageDO;
import com.ouheng.shop.domain.CmsDO;
import com.ouheng.shop.service.ArticleImageService;
import com.ouheng.shop.service.CmsService;

/**
 * 鲜语
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-06-22 10:58:35
 */
 
@Controller
@RequestMapping("/shop/cms")
public class CmsController {
	@Autowired
	private CmsService cmsService;
	
	@Autowired
	private ArticleImageService articleImageService;
	
	@GetMapping()
	@RequiresPermissions("shop:cms:cms")
	String Cms(){
	    return "shop/cms/cms";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:cms:cms")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CmsDO> cmsList = cmsService.list(query);
		int total = cmsService.count(query);
		PageUtils pageUtils = new PageUtils(cmsList, total);
		return pageUtils;
	}
	
	@Log("添加鲜语")
	@GetMapping("/add")
	@RequiresPermissions("shop:cms:add")
	String add(){
	    return "shop/cms/add";
	}

	@Log("编辑鲜语")
	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:cms:edit")
	String edit(@PathVariable("id") Long id,Model model){
		CmsDO cms = cmsService.get(id);
		model.addAttribute("cms", cms);
	    return "shop/cms/edit";
	}
	
	@Log("添加鲜语图片")
	@GetMapping("/image/{id}")
	@RequiresPermissions("shop:cms:image")
	String image(@PathVariable("id") Long id,Model model){
		CmsDO cms = cmsService.get(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cmsid", id);
		List<ArticleImageDO> listImage = articleImageService.list(map);
		model.addAttribute("cms", cms);
		model.addAttribute("listImage", listImage);
	    return "shop/cms/image";
	}
	
	/**
	 * 保存
	 */
	@Log("保存鲜语")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:cms:add")
	public R save( CmsDO cms){
		if(cmsService.save(cms)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 
	 * @Title: saveImage   
	 * @Description: TODO(保存图片)   
	 * @param: @param product
	 * @param: @param request
	 * @param: @return      
	 * @return: R      
	 * @throws
	 */
	@Log("保存鲜语图片")
	@ResponseBody
	@PostMapping("/saveImage")
	@RequiresPermissions("shop:cms:edit")
	public R saveImage( CmsDO cms,HttpServletRequest request){
		List<Object> list;
		try {
			list = ServletsUtils.convertObjectList(ArticleImageDO.class, request,"attachs_");
			cmsService.saveImage(cms, list);
			return R.ok();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return R.error();
	}
	
	/**
	 * 修改
	 */
	@Log("修改鲜语")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:cms:edit")
	public R update( CmsDO cms){
		cmsService.update(cms);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Log("删除鲜语")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:cms:remove")
	public R remove( Long id){
		if(cmsService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@Log("删除鲜语")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:cms:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		cmsService.batchRemove(ids);
		return R.ok();
	}
	
}
